
/**************************************************************************************************
Filename:       NewbitBinding_Demo.c
Editor:         Tome @ newbit
Revised:        $Date: 2017/3/22 11:20:02 +0800  $
Revision:       $Revision: 00001 $

Description:    
History:        
Notes:          share on github.com/newbitstudio
support on bbs.newbitstudio.com

**************************************************************************************************/



/**************************************************************************************************
// INCLUDES
**************************************************************************************************/
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"


#if !defined( WIN32 )
#include "OnBoard.h"
#endif

#include "ul_strings.h"

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"


#include "NewbitBinding_Demo.h"


/**************************************************************************************************
// TYPEDEF
**************************************************************************************************/



/**************************************************************************************************
// CONSTANTS
**************************************************************************************************/

#define TASK_ID_SELF NewbitBinding_Demo_TaskID




/**************************************************************************************************
// LOCAL VERIABLE
**************************************************************************************************/
byte NewbitBinding_Demo_TaskID;   // Task ID for internal task/event processing

devStates_t NewbitDemo_NwkState;

byte NewbitDemo_TransID;  // This is the unique message ID (counter)



extern const SimpleDescriptionFormat_t NewbitDemo_SimpleDesc;


// This is the Endpoint/Interface description.  It is defined here, but
// filled-in in NewbitDemo_Init().  Another way to go would be to fill
// in the structure here and make it a "const" (in code space).  The
// way it's defined in this sample app it is define in RAM.
endPointDesc_t NewbitDemo_epDesc;


/**************************************************************************************************
// FUNCTIONS DECLERATION
**************************************************************************************************/


//static void NewbitBinding_Demo_HandleKeys( uint8 shift, uint8 keys );

extern void nb_demo_init(void);
extern void nb_demo_HandleKeys(uint8 shift, uint8 keys);
extern void nb_demo_ProcessZDOMsgs(zdoIncomingMsg_t *inMsg);
extern void nb_demo_ProcessRvdMsgs(afIncomingMSGPacket_t* inMsg);

/**************************************************************************************************
// FUNCTIONS
**************************************************************************************************/

/*********************************************************************
* @fn      NewbitBinding_Demo_Init
*
* @brief   Initialization function for the Generic App Task.
*          This is called during initialization and should contain
*          any application specific initialization (ie. hardware
*          initialization/setup, table initialization, power up
*          notificaiton ... ).
*
* @param   task_id - the ID assigned by OSAL.  This ID should be
*                    used to send messages and set timers.
*
* @return  none
*/
void NewbitBinding_Demo_Init( uint8 task_id )
{
  TASK_ID_SELF = task_id;  
  
  NewbitDemo_NwkState = DEV_INIT;
  NewbitDemo_TransID = 0;
  

  // Fill out the endpoint description.
  NewbitDemo_epDesc.endPoint = NEWBITDEMO_ENDPOINT;
  NewbitDemo_epDesc.task_id = &TASK_ID_SELF;
  NewbitDemo_epDesc.simpleDesc
    = (SimpleDescriptionFormat_t *)&NewbitDemo_SimpleDesc;
  NewbitDemo_epDesc.latencyReq = noLatencyReqs;
  
  // Register the endpoint description with the AF
  afRegister( &NewbitDemo_epDesc );
  

  
  // Register for all key events - This app will handle all key events
  RegisterForKeys( TASK_ID_SELF );
  
  
  ZDO_RegisterForZDOMsg( TASK_ID_SELF, End_Device_Bind_rsp );
  ZDO_RegisterForZDOMsg( TASK_ID_SELF, Match_Desc_rsp );  
  ZDO_RegisterForZDOMsg( TASK_ID_SELF, Device_annce );  
  ZDO_RegisterForZDOMsg( TASK_ID_SELF, Simple_Desc_rsp );    
  ZDO_RegisterForZDOMsg( TASK_ID_SELF, Bind_rsp );
  ZDO_RegisterForZDOMsg( TASK_ID_SELF, Unbind_rsp );
  
  
  nb_demo_init();
}




/*********************************************************************
* @fn      NewbitBinding_Demo_ProcessEvent
*
* @brief   Generic Application Task event processor.  This function
*          is called to process all events for the task.  Events
*          include timers, messages and any other user defined events.
*
* @param   task_id  - The OSAL assigned task ID.
* @param   events - events to process.  This is a bit map and can
*                   contain more than one event.
*
* @return  none
*/
uint16 NewbitBinding_Demo_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  
  
  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( NewbitBinding_Demo_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {
      case ZDO_CB_MSG:
        nb_demo_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
        break;
        
      case KEY_CHANGE:
        nb_demo_HandleKeys(((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
        break;
        
      ///case UART_EVT:
          // read data
      // break;
        
      case AF_DATA_CONFIRM_CMD:
        
        break;
        
      case AF_INCOMING_MSG_CMD:
        nb_demo_ProcessRvdMsgs(MSGpkt);
        break;
        
      case ZDO_STATE_CHANGE:
        NewbitDemo_NwkState = (devStates_t)(MSGpkt->hdr.status);
        
        /*
        if ( ( NewbitBinding_Demo_NwkState == DEV_ROUTER )          
        || ( NewbitBinding_Demo_NwkState == DEV_END_DEVICE )  )
        osal_start_reload_timer(TASK_ID_SELF, NBD_EVT_DHT11, 1000 ); 
        */
        
        break;
        
      case ONEBUS_EVENT:
        
        break;
        
      default:
        break;
      }
      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );
      
      // Next
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( TASK_ID_SELF );
    }
    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  
  
  
  if ( events & NB_DMOETASK_EVT )
  {
    
    return ( events ^ NB_DMOETASK_EVT );
  }
  
  
  return 0;
}



/**************************************************************************************************
Copyright 2016-2017 Newbit Studio. All rights reserved.
**************************************************************************************************/



